package com.pmp.controller.activiti;

import com.pmp.activiti.editor.BpmnModelConvert2OrgChart;
import com.pmp.vo.OrgChart;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @description: 流程展示管理
 * @author: pmq
 * @time: 2017-05-30 23:49
 */
@Controller
@RequestMapping("/activiti/processManager")
public class ProcessManager {

    public final static String path = "uploadFile";

    @RequestMapping("/")
    public String toProceShowJsp(){
        return "/activiti/proceShow";
    }

    @RequestMapping("/getOrgChartList")
    @ResponseBody
    public OrgChart getOrgChartList(){
        BpmnModelConvert2OrgChart bpmn2OrgChart = new BpmnModelConvert2OrgChart();
        List<OrgChart> orgCharts = bpmn2OrgChart.bpmnConvertoModel();
        if(orgCharts != null && orgCharts.size()>0){
            OrgChart root = orgCharts.get(0);
            orgCharts.remove(0);
            return bpmn2OrgChart.constructOrgChart(root,orgCharts);
        }
        return null;
    }

    @RequestMapping("/getRecordInfo")
    public String toRecordInfo(){
        return "/activiti/procInfo";
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
    public Map<String,String> uploadFile(HttpServletRequest request,
                             String[] params){

        Map<String,String> resultMap = new HashMap<>();
        MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
        Map<String,String[]> paramMap = mRequest.getParameterMap();
        Map<String, MultipartFile> fileMap = mRequest.getFileMap();
        String lenFile = paramMap.get("chunks")[0];
        String fileName = paramMap.get("name")[0];
        String lenNow = paramMap.get("chunk")[0];
        String uuidFile = paramMap.get("uuid")[0];
        String uploadDir = request.getSession().getServletContext()
                .getRealPath("/")
                + path;
        File file = new File(uploadDir);

        if (!file.exists()) {
            file.mkdir();
        }
        FileOutputStream out = null;
        InputStream in = null;
        try{
            int i = 0;

            byte[] buffer = new byte[1024];
            for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet()
                    .iterator(); it.hasNext(); i++) {

                Map.Entry<String, MultipartFile> entry = it.next();
                MultipartFile mFile = entry.getValue();

                if(Integer.valueOf(lenFile) == 1){
                    fileName = mFile.getOriginalFilename();
                    System.out.println("file:" + mFile.getName());;
                    System.out.println("fileType:" + mFile.getContentType());;
                }
                if(Integer.valueOf(lenNow) == 0){
                    out = new FileOutputStream(uploadDir + "/" + fileName);
                }else{
                    out = new FileOutputStream(uploadDir + "/" + fileName,true);
                }
                in = mFile.getInputStream();
                int len = 0;
                while((len = in.read(buffer)) > 0){
                    out.write(buffer,0,len);
                }
            }

            System.out.println(fileName);
            resultMap.put("result","ok");
            resultMap.put("id",fileName);
            resultMap.put("url",uploadDir + "/" + fileName);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(in != null)
                    in.close();
                if(out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return resultMap;
    }

}
